#coding=utf-8
'''
Created on May 11, 2016
@author: wangpeng
'''

import unittest
from appium import webdriver
from selenium.webdriver.common.by import By   #用于匹配某个值是否出现，智能等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import common.config
import common.logging_save_huawei_honor4A
import common.desired_setting_huawei_honor4A
import threading
import re
import common.Create_Log_Folder
import os
import os.path
import common.logging_save_huawei_honor4A
import serial
#import common.save_serial_log
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import subprocess

class Test(unittest.TestCase):
    def setUp(self):
        os.system('taskkill /f /im adb.exe')
        common.logging_save_huawei_honor4A.logging.info("app测试开始，正在开始启动app")
        print time.ctime(),"app测试开始，正在开始启动app"
        self.begin_time_for_serial = time.strftime("%y%m%d_%H:%M:%S", time.localtime()) 
        self.save_log = common.Create_Log_Folder.Create("\\huawei_honor4A\\temperature_error")    #建立report保存路径
        common.logging_save_huawei_honor4A.logging.info("进行温度是否会变成异常值测试")
        print time.ctime(),"进行温度是否会变成异常值测试"
        sys.path.append('\adbcommand')  #增加adb log
        self.test = subprocess.Popen(r'E:\eclipse\huawei_rader\all_mobile\testcase\huawei_honor4A\adbcommond\temperature_error.bat') #导入adb
        time.sleep(1)
        
        #开启appium设置
        self.desired_setting = common.desired_setting_huawei_honor4A.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_setting)
        common.logging_save_huawei_honor4A.logging.info("app连接BLE中，70秒后再做判定。。。")
        print time.ctime(),"app连接BLE中，70秒再做判定。。。"
        time.sleep(70)
        try:
            self.driver.find_element_by_id("com.huawei.smallRadar:id/sure").click()
            time.sleep(3)
            common.logging_save_huawei_honor4A.logging.info("close弹出框")
            print time.ctime(),"close弹出框"

        except:
            pass

        
    def tearDown(self):
        self.driver.quit()
        time.sleep(1)
        self.test.kill()
        os.system('taskkill /f /im adb.exe')
        print '关闭adb log'
        time.sleep(10)
        print 'finished'
        
    def test_play(self):
        u"""测试步骤：反复数据，然后获取温度值，观察是否出现99度异常现象 ，刷新200次，每次间隔20秒"""
        savenumber = 1
        save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        #判断是否进入主界面
        try:
            WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.ID,common.config.wendu()))) #确认是否进入到主界面                       
            common.logging_save_huawei_honor4A.logging.info("经过确认，已经进入到主菜单页面")
            print time.ctime(),"经过确认，已经进入到主菜单页面"
            time.sleep(3)
        except:
            self.driver.get_screenshot_as_file(save_pic)
        
        time.sleep(1)
        #检测是否出现异常
        testtime = 1
        while(testtime <=50):
            time.sleep(1)
            self.driver.swipe(500, 500, 500, 1000, 5000)   #刷新数据
            time.sleep(20)
            normal_value = [21,22,23,24,25,26,27,28,29,30,31,32,33,34,35]
            current = self.driver.find_element_by_id(common.config.wendu()).text
            current_temp = int(current)    #强制转换为整数型，用于整数值的比较
            common.logging_save_huawei_honor4A.logging.info("当前获取到温度值为："+str(current_temp)+"度")
            print time.ctime(),"当前获取到温度值为："+str(current_temp)+"度"  
            time.sleep(5)
            try:
                self.assertTrue(current_temp in normal_value, "检测到温度值异常")   
                common.logging_save_huawei_honor4A.logging.info("温度值显示正常，测试次数为第：" +str(testtime)+"次")
                print time.ctime(),"温度值显示正常，没有出现99度等异常值"    
                
            except:
                self.driver.get_screenshot_as_file(save_pic)   
                self.fail("检测到严重和当前环境不一致的值出现")
            
            testtime = testtime + 1

if __name__ == "__main__":
    unittest.main()